package com.shaozi.nc.rpc;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;

import com.shaozi.data.rpc.base.BaseRPC;
import com.shaozi.lib.request.RequestCallback;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.manage.bean.CheckUpdateCompany;
import com.shaozi.utils.common.JsonUtil;

import hprose.client.HproseHttpClient;
import hprose.common.HproseCallback;
import hprose.common.HproseErrorEvent;

public class UserInfoRPC extends BaseRPC {
	private Logger logger = Logger.getLogger(UserInfoRPC.class);
	@Value("${sys.info.api}")
	private String domain;

	public UserInfoRPC() {
		super();
	}

	@Override
	public String getDomain() {
		return domain;
	}

	@Override
	public String getUrl() {
		return "Server/User";
	}

	@Override
	public void doRequest(RequestCallback callback, Object... params) {
		final String companyId = (String) params[0];
		long lastUpdateTime = (long) params[1];
		HproseHttpClient client = this.getClient();
		client.setHeader("companyId", companyId);
		client.setHeader("requestType", "rpc");
		asyncRequest(client, callback, lastUpdateTime, companyId);
	}

	/**
	 * 同步请求
	 */
	private void syncRequest(HproseHttpClient client, RequestCallback callback, long lastUpdateTime, String companyId) {
		try {
			client.useService(IUserInfo.class);
			CheckUpdateInfoResponse result = (CheckUpdateInfoResponse) client.invoke("result");
			callback.callback(result.getStatus(), result.getData());
		} catch (Throwable e) {
			logger.error(companyId + ",lastUpdateTime=" + lastUpdateTime + "," + e.getMessage() + "\n"
					+ SzException.getStackMessage(e));
			callback.callback(false, null);
		}
	}

	/**
	 * 异步请求
	 */
	private void asyncRequest(HproseHttpClient client, RequestCallback callback, long lastUpdateTime,
			String companyId) {
		final IUserInfo userInfo = (IUserInfo) client.useService(IUserInfo.class);
		userInfo.listOfCompany(companyId, lastUpdateTime, new HproseCallback<CheckUpdateInfoResponse>() {
			@Override
			public void handler(CheckUpdateInfoResponse result, Object[] arguments222) {
				logger.info(String.format("CheckUpdateInfoResponse=%s, current timestamp =%d",
						JsonUtil.toJsonString(result), lastUpdateTime));
				callback.callback(result.getStatus(), result.getData());
				if (!result.getStatus()) {
					logger.error(result.getMsg());
				}
			}

		}, new HproseErrorEvent() {

			@Override
			public void handler(String name, Throwable error) {
				logger.error(companyId + ",lastUpdateTime=" + lastUpdateTime + "," + error.getMessage() + "\n"
						+ SzException.getStackMessage(error));
				callback.callback(false, null);
			}
		});
	}

	private interface IUserInfo {
		void listOfCompany(String companyId, long lastUpdateTime, HproseCallback<CheckUpdateInfoResponse> callback,
				HproseErrorEvent errorEvent);
	}

	public class CheckUpdateInfoResponse {
		private boolean status;
		private String msg;
		private CheckUpdateCompany data;

		public boolean getStatus() {
			return status;
		}

		public void setStatus(boolean status) {
			this.status = status;
		}

		public String getMsg() {
			return msg;
		}

		public void setMsg(String msg) {
			this.msg = msg;
		}

		public CheckUpdateCompany getData() {
			return data;
		}

		public void setData(CheckUpdateCompany data) {
			this.data = data;
		}
	}

}
